package cn.edu.hust.sse.software7.class3.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.hust.sse.software7.class3.entities.EBAdmin;
import cn.edu.hust.sse.software7.class3.entities.EBCategory;
import cn.edu.hust.sse.software7.class3.entities.EBCommonOnProduct;
import cn.edu.hust.sse.software7.class3.entities.EBProduct;
import cn.edu.hust.sse.software7.class3.entities.EBUser;
import cn.edu.hust.sse.software7.class3.service.EBAdminService;
import cn.edu.hust.sse.software7.class3.service.EBCategoryService;
import cn.edu.hust.sse.software7.class3.service.EBProductService;
import cn.edu.hust.sse.software7.class3.service.EBUserService;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBAdminServiceImpl;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBCategoryServiceImpl;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBCommonOnProductServiceImpl;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBProductServiceImpl;
import cn.edu.hust.sse.software7.class3.serviceImpl.EBUserServiceImpl;

public class GoToServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	EBAdminService ebs = new EBAdminServiceImpl();
	EBCategoryService ebcs = new EBCategoryServiceImpl();
	EBProductService ebps = new EBProductServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String page = req.getParameter("page");
		String message = "";
		if (page.equals("addadmin")) {
			resp.sendRedirect("admin/adminManage/addAdmin.jsp");
		} else if (page.equals("watchadmins")) {
			HttpSession session = req.getSession();
			List<EBAdmin> admins = ebs.getAdmins();
			if (null == admins || admins.isEmpty()) {
				message = "没有管理员";
				session.setAttribute("message", message);
				resp.sendRedirect("admin/message.jsp");
			} else {
				session.setAttribute("adminList", admins);
				resp.sendRedirect("admin/adminManage/watchAdmin.jsp");
			}
		} else if (page.equals("updateadmin")) {
			HttpSession session = req.getSession();
			String admin2Update = req.getParameter("admin2Update");
			Long admin2UpdateId = new Long(admin2Update);
			EBAdmin admin = ebs.getAdminById(admin2UpdateId);
			session.setAttribute("admin2Update", admin);
			resp.sendRedirect("admin/adminManage/updateAdmin.jsp");
		} else if (page.equals("watchcategories")) {
			HttpSession session = req.getSession();
			List<EBCategory> categories = ebcs.getCategories();
			session.setAttribute("categories", categories);
			resp.sendRedirect("admin/productManage/watchCategories.jsp");
		} else if (page.equals("addcategoty")) {
			Long parentCategoryId = null;
			HttpSession session = req.getSession();
			session.removeAttribute("AddCategoryResult");
			try {
				parentCategoryId = new Long(
						req.getParameter("parentCategoryId"));
			} catch (Exception e) {
				// TODO: handle exception
			}
			if (null != parentCategoryId) {
				EBCategory parentCategory = ebcs
						.getCategoryById(parentCategoryId);
				session.setAttribute("parentCategory", parentCategory);
				resp.sendRedirect("admin/productManage/addCategory.jsp");
			} else {
				session.setAttribute("parentCategory", null);
				resp.sendRedirect("admin/productManage/addCategory.jsp");
			}
		} else if (page.equals("watchProds")) {
			EBProductService ebps = new EBProductServiceImpl();
			HttpSession session = req.getSession();
			List<EBProduct> prods = ebps.getProds();
			if (null == prods || prods.isEmpty()) {
				session.removeAttribute("prodList");
			} else {
				session.setAttribute("prodList", prods);
			}
			resp.sendRedirect("admin/productManage/watchProd.jsp");
		} else if (page.equals("watchProdDetail")) {
			String prod2Watch = req.getParameter("prod2Watch");
			HttpSession session = req.getSession();
			Long prodId = new Long(prod2Watch);
			EBProduct ebp = ebps.getProdById(prodId);
			session.setAttribute("watchProd", ebp);
			List<EBCommonOnProduct> ebcop = new EBCommonOnProductServiceImpl()
					.getCommonsByProductId(prodId);
			session.setAttribute("prodCommonList", ebcop);
			resp.sendRedirect("admin/productManage/watchProdDetail.jsp");
		} else if (page.equals("addProd")) {
			String prod2Add = req.getParameter("categoryId");
			HttpSession session = req.getSession();
			Long categoryId = new Long(prod2Add);
			EBCategory category = ebcs.getCategoryById(categoryId);
			session.setAttribute("categoryOP", category);
			resp.sendRedirect("admin/productManage/addProd.jsp");
		} else if (page.equals("updateProd")) {
			String prod2Update = req.getParameter("prod2Update");
			Long prodId = new Long(prod2Update);
			EBProductService ebps = new EBProductServiceImpl();
			EBProduct prod = ebps.getProdById(prodId);
			HttpSession session = req.getSession();
			session.setAttribute("updateProduct", prod);
			resp.sendRedirect("admin/productManage/updateProd.jsp");
		}else if(page.equals("watchusers")){
			HttpSession session = req.getSession();
			String range = req.getParameter("range");
			Integer rangeValue = new Integer(range);
			EBUserService ebus = new EBUserServiceImpl();
			Integer userSize = ebus.getAllUsersSize();
			List<EBUser> users = ebus.getAllUsers(rangeValue);
			session.setAttribute("uSize", userSize);
			session.setAttribute("users", users);
			resp.sendRedirect("admin/infoManage/watchUsers.jsp");
		}

	}

}
